using System.Linq;
using Admin3000.Backend.Api.Entity;
using Admin3000.Backend.Api.Params;
using Admin3000.Backend.Api.Repository;
using Admin3000.Backend.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Admin3000.Backend.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class CommentsController : ControllerBase
    {
        private IConfiguration _configuration;

        private IRepository<Comments> _commentsRepository;

        private TokenParameter _tokenParameter;

        public CommentsController(
            IConfiguration configuration,
            IRepository<Comments> commentsRepository
        )
        {
            _configuration = configuration;
            _commentsRepository = commentsRepository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }



        //列出评论列表
        [HttpGet]
        public dynamic Get()
        {
            var comments = _commentsRepository.Table.ToList();
            var res = new
            {
                Code = 1000,
                Data = new { Data = comments },
                Msg = "获取评论列表成功^_^"
            };

            return JsonHelper.Serialize(res);
        }

        //按评论Id搜索
        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var comments = _commentsRepository.GetById(id);

            return new
            {
                Code = 1000,
                Data = comments,
                Msg = "获取成功"
            };
        }


        //增加评论 评论内容
        [HttpPost]
        public dynamic Post(CreateComments newComments)
        {
            // commenttext = newComments.CommentText.Trim();
            var Comments = new Comments
            {
                CommentText = newComments.CommentText,
            };
            _commentsRepository.Insert(Comments);
            return new
            {
                Code = 1000,
                Data = Comments,
                Msg = "增加评论成功"
            };
        }


        //删除对应Id评论
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            _commentsRepository.Delete(id);
            return new { Code = 1000, Data = "", Mig = "删除评论成功" };
        }

        //获取评论数量
        [HttpGet("homepage"), Route("homepage")]
        public dynamic homepage()
        {
            var arictles = _commentsRepository.Table.ToList();
            var res = new
            {
                Code = 1000,
                Data = new { Data = arictles },
                Msg = "获取评论列表成功^_^"
            };

            return JsonHelper.Serialize(res);
        }
        //查询评论id
        // [HttpGet("{id}")]
        // public dynamic Get(int id)
        // {
        //     var comments = _commentsRepository.GetById(id);

        //     return new
        //     {
        //         Code = 1000,
        //         Data = comments,
        //         Msg = "获取成功"
        //     };

        // }
    }
    }
